package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiangyi.teachingsystem.dao.TaskMapper;
import com.xiangyi.teachingsystem.entity.po.Course;
import com.xiangyi.teachingsystem.entity.po.Task;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.CourseService;
import com.xiangyi.teachingsystem.service.TaskService;
import com.xiangyi.teachingsystem.validation.groups.TaskInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
public class TaskController {
    @Autowired
    TaskService taskService;
    @Autowired
    CourseService courseService;

    @PostMapping("/addTask")
    public Result addTask(@Validated @RequestBody Task task){
        Course courseInfo = courseService.getById(task.getCourseId());
        if (courseInfo==null)
            return Result.failure("课程不存在，请检查courseID");
        Task name = taskService.getOne(new QueryWrapper<Task>()
                .eq("task_name", task.getTaskName())
                .eq("course_id",task.getCourseId()));
        if (name!=null)
            return Result.failure("任务名称已存在");
        try {
            taskService.save(task);
            return Result.success("任务添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/updateTask")
    public Result updateTask(@Validated(TaskInfo.class) @RequestBody Task task){
        Task taskInfo = taskService.getById(task.getId());
        if (taskInfo==null)
            return Result.failure("任务不存在，请检查id");
        if (!Objects.equals(taskInfo.getTaskName(), task.getTaskName())){
            Task task_name = taskService.getOne(new QueryWrapper<Task>().eq("task_name", task.getTaskName()));
            if (task_name!=null)
                return Result.failure("任务名称已存在");
        }
        try {
            taskService.updateById(task);
            return Result.success("任务信息更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/taskList")
    public Result taskList(@RequestParam Integer courseId){
        try {
            List<Task> taskByCourseId = taskService.getTaskByCourseId(courseId);
            return Result.success("任务列表查询成功").setData(taskByCourseId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误");
        }
    }

    @GetMapping("/delTask")
    public Result delTask(@RequestParam Integer taskId){
        try {
            taskService.delTask(taskId);
            return Result.success("任务删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误");
        }
    }
    @GetMapping("/task/getById")
    public Result getById(@RequestParam Integer taskId){
        return Result.success("小组信息查询成功").setData(taskService.getById(taskId));
    }
}
